package others.swing.p019;

import java.sql.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Color;
import javax.swing.*;

public class Change extends JFrame implements ActionListener {
    JLabel p1, p2, p3, p4, p5, p6;//控件的定义
    TextField t1, t2, t3;
    JButton btn1, btn2, btn3, btn4;
    JTextArea ta;
    int p = 1;

    //构造函数
    Change() {
        this.setTitle("修改密码！");
        p1 = new JLabel("请输入账号：");
        p2 = new JLabel("请输入原始密码：");
        p3 = new JLabel("是否进入其他系统？请选择：");
        p5 = new JLabel("修改后信息如下：");
        p6 = new JLabel("请输入新密码");

        t3 = new TextField(10);
        t3.setEchoChar('*');
        t1 = new TextField(10);

        t2 = new TextField(10);
        t2.setEchoChar('*');

        ta = new JTextArea(10, 20);
        ta.setBackground(Color.cyan);

        btn1 = new JButton("确定");
        btn2 = new JButton("取消");
        btn3 = new JButton("重新登录");
        btn4 = new JButton("查询信息");

        t1.addActionListener(this);//为各控件注册监听器
        t2.addActionListener(this);
        t3.addActionListener(this);

        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);

        Container c = getContentPane();
        c.setLayout(null);

        c.add(p1);
        c.add(p2);
        c.add(p3);
        c.add(p5);
        c.add(p6);

        c.add(t1);
        c.add(t2);
        c.add(t3);

        c.add(ta);

        c.add(btn1);
        c.add(btn2);
        c.add(btn3);
        c.add(btn4);

        p1.setBounds(50, 10, 100, 40);
        p2.setBounds(50, 50, 100, 40);
        p3.setBounds(50, 290, 250, 40);
        p5.setBounds(50, 160, 250, 40);

        ta.setBounds(50, 190, 220, 110);
        ta.setBackground(Color.cyan);


        t1.setBounds(170, 10, 100, 30);
        t2.setBounds(170, 50, 100, 30);
        t3.setBounds(170, 90, 100, 30);
        p6.setBounds(50, 90, 100, 30);


        btn1.setBounds(80, 130, 80, 30);
        btn2.setBounds(160, 130, 80, 30);
        btn3.setBounds(50, 330, 100, 30);
        btn4.setBounds(150, 330, 100, 30);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e1) {
                dispose();
                System.exit(0);
            }
        });
        this.setBounds(300, 50, 360, 400);
        this.setVisible(true);
        this.setResizable(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        p = 1;
        Connection con;
        Statement sql;
        ResultSet rs;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException e1) {
            System.out.println(" " + e1);
        }
        if ("确定".equals(e.getActionCommand())) {
            try {
                con = DriverManager.getConnection("jdbc:odbc:campus-card", "", "");
                sql = con.createStatement();
                String spwd, sno, sname, pwd, type, money, updateStr, c;
                spwd = t2.getText();
                pwd = t3.getText();
                c = t1.getText();
                rs = sql.executeQuery("SELECT * FROM message WHERE sno ='" + c + "'");

                if (rs.next()) {
                    String oldPwd = rs.getString(3);
                    if (oldPwd.equals(pwd)) {
                        System.out.println(p);
                        updateStr = "UPDATE message SET pwd=" + "'" + pwd + "'" + " WHERE sno = " + "'" + c + "'";//更新数据库
                        sql.executeUpdate(updateStr);
                        rs = sql.executeQuery("SELECT * FROM message WHERE sno ='" + c + "'");
                        //输出信息
                        while (rs.next()) {
                            sno = rs.getString(1);
                            sname = rs.getString(2);
                            pwd = rs.getString(3);
                            type = rs.getString(4);
                            money = rs.getString(5);
                            ta.append("学号:" + sno + '\n');
                            ta.append("姓名:" + sname + '\n');
                            ta.append("密码:" + pwd + '\n');
                            ta.append("身份:" + type + '\n');
                            ta.append("余额:" + money + '\n');
                        }
                    } else {
                        ta.append("密码错误！请重新输入！" + '\n');
                    }
                }else {
                    ta.append("查无此人！" + '\n');
                }
                con.close();
            } catch (SQLException e1) {
                System.out.println(e1);
            }
        }
        /*其他按钮操作*/
        else if (e.getActionCommand().equals("重新登录")) {
            new Load().setVisible(true);
            this.dispose();
        } else if (e.getActionCommand().equals("查询信息")) {
            new Search().setVisible(true);
            this.dispose();
        } else if (e.getActionCommand().equals("取消")) {
            dispose();
            System.exit(0);
        }
    }

    public static void main(String args[]) {
        new Change();
    }

}

